package com.LeeCode;

/**
 * 子数组最大平均数 I
 */

public class Code643 {
    public static void main(String[] args) {
        int[] nums = {1, 12, -5, -6, 50, 3};
        int k = 4;
        System.out.println(new Code643().findMaxAverage(nums, k));

    }

    public double findMaxAverage(int[] nums, int k) {
        int sum = 0, maxSum;
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        maxSum = sum;

        for (int i = k; i < nums.length; i++) {
            sum += nums[i] - nums[i - k];
            maxSum = Math.max(maxSum, sum);
        }
        return maxSum * 1.0 / k;
    }
}
